﻿Imports DataSet_apaTableAdapters
Imports DataSet_apa
Imports System.Data.SqlClient
Partial Class Reportes_ReporteGrid
    Inherits System.Web.UI.Page
    Dim util As New utileria
    Dim sSQL, campos As String
    Dim idTable As New filtroDataTable
    Dim idAdapt As New filtroTableAdapter
    Protected Sub Reportes_ReporteGrid_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim swhere As String

        campos = Request.QueryString("campos")
        swhere = util.sWhereFiltro(Session("filtro"))
        sSQL = "Select  " + campos + " from animal " + swhere
        'sSQL = "Select COUNT(*) AS TotalReg from animal  where (select " + campos + " from animal " + swhere + ")"
        Try
            Dim sdsAnimales As New SqlDataSource
            ' Dim sConectionString As String
            ' sConectionString = ConfigurationManager.ConnectionStrings.Item("GanaderiaConnectionString").ToString
            sdsAnimales.ConnectionString = ConfigurationManager.ConnectionStrings.Item("GanaderiaConnectionString").ToString
            sdsAnimales.SelectCommand = sSQL
            sdsAnimales.DataBind()

            GridView1.DataSource = sdsAnimales
            GridView1.DataBind()
            'LbComenta.Text = GridView1.Rows.Count
        Catch ex As Exception

        End Try
        ID = Session("filtro")
        idTable = idAdapt.GetFiltroById(ID) ' -- Cargar el encabezado del filtro
        LbFiltro.Text = idTable(0).nombreFiltro

        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim CampoReg As String
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select  COUNT(*) AS TotalReg from animal " + swhere
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()

            If Lector.Read() Then
                CampoReg = Lector("TotalReg")
            End If
        Catch ex As Exception
        End Try
        LbComenta.Text = "Numero de registros " + CampoReg.ToString
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        Dim sdsAnimales As New SqlDataSource
        Dim newPageNumber1 As Integer = e.NewPageIndex
        GridView1.PageIndex = newPageNumber1
        sdsAnimales.ConnectionString = ConfigurationManager.ConnectionStrings.Item("GanaderiaConnectionString").ToString
        sdsAnimales.SelectCommand = sSQL
        sdsAnimales.DataBind()

        GridView1.DataSource = sdsAnimales
        GridView1.DataBind()
    End Sub
End Class
